package com.hb.game.pk.adpter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.beans.ErBaQzM2Bean;
import com.hb.game.pk.beans.ErBaQzType1Bean;
import com.hb.game.pk.beans.ErBaQzType4Bean;
import com.hb.game.pk.beans.XZBean;
import com.hb.game.pk.utils.SpannableStringUtils;
import com.hb.game.pk.utils.StringUtils;

import java.util.List;
import java.util.Map;

public class SZWWChatAdapter extends BaseAdapter {

    private ListView mListView;
    private Activity act;
    private List<Map> data;
    private Gson gson = new Gson();

    public SZWWChatAdapter(Activity act, ListView rvList) {
        this.act = act;
        mListView = rvList;
    }

    public void setNewData(List<Map> data) {
        this.data = data;
        notifyDataSetChanged();
        mListView.setSelection(getCount() - 1);
    }

    public void addMore(Map map) {
        this.data.add(map);
        notifyDataSetChanged();
        mListView.setSelection(mListView.getCount() - 1);
    }

    public void setStatus(int position) {
        Map map = data.get(position);
        map.put("cliced", true);
        data.set(position, map);
        notifyDataSetChanged();
    }

    public List<Map> getData() {
        return this.data;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(act, R.layout.item_szww_chat_list, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.llZJList.setVisibility(View.GONE);
        holder.rlRedLeft.setVisibility(View.GONE);
        holder.rlRedRight.setVisibility(View.GONE);
        Map item = data.get(position);
        if (item.containsKey("m")) {
            double m = Double.parseDouble(item.get("m").toString());
            if (m == 1.0) {
                if (Double.parseDouble(item.get("type").toString()) == 4.0) {
                    String s = gson.toJson(item);
                    ErBaQzType4Bean erBaQzType4Bean = gson.fromJson(s, ErBaQzType4Bean.class);
                    ErBaQzType4Bean.DataBean data = erBaQzType4Bean.getData();
                    String trim = "";
                    if (!StringUtils.isEmpty(data.getAvatar()))
                        trim = StringUtils.getString(data.getAvatar());
                    int res = 0;
                    if (StringUtils.isEmpty(trim)) {
                        res = R.mipmap.ic_head_normal;
                    } else {
                        int i = Integer.parseInt(trim);
                        res = APP.headResource.get(i - 1);
                    }
//                    float money = Float.parseFloat(data.getMoney()) / 100;
                    if (data.getUser_id().equals(APP.uid)) {
                        holder.rlRedRight.setVisibility(View.VISIBLE);
                        holder.tvRedNameRight.setText(data.getUser_id());
                        holder.ivRedHeadRight.setBackgroundResource(res);
                        holder.tvRedMoneyRight.setText("比分红包");
//                        holder.tvRedMoneyRight.setText(money + "");
                        if (item.containsKey("cliced")) {
                            holder.llRedBoxRight.setBackgroundResource(R.drawable.bg_chat_item_shape_red_clicked);
                            holder.ivRedRight.setBackgroundResource(R.mipmap.ic_chat_right_cliced);
                        }
                    } else {
                        holder.rlRedLeft.setVisibility(View.VISIBLE);
                        holder.tvRedNameLeft.setText(data.getUser_id());
                        holder.ivRedHeadLeft.setBackgroundResource(res);
                        holder.tvRedMoneyLeft.setText("比分红包");
//                        holder.tvRedMoneyLeft.setText(money + "");
                        if (item.containsKey("cliced")) {
                            holder.llRedBoxLeft.setBackgroundResource(R.drawable.bg_chat_item_shape_red_clicked);
                            holder.ivRedLeft.setBackgroundResource(R.mipmap.ic_chat_left_clicked);
                        }
                    }
                }
            } else {
                holder.llZJList.setVisibility(View.VISIBLE);
                holder.llZJList.removeAllViews();
                String s = gson.toJson(item);
                ErBaQzM2Bean erBaQzM2Bean = gson.fromJson(s, ErBaQzM2Bean.class);
                ErBaQzM2Bean.DataBean titleData = erBaQzM2Bean.getData();
                ErBaQzM2Bean.DataBean.ZhuangInfoBean zhuang_info = titleData.getZhuang_info();
                List<ErBaQzM2Bean.DataBean.XiazhuInfoBean> xiazhu_info = titleData.getXiazhu_info();
                TextView zTextView = new TextView(act);
                String hb_money = "";
                if (!StringUtils.isEmpty(zhuang_info.getHb_money()))
                    hb_money = Float.parseFloat(zhuang_info.getHb_money()) / 100 + "";
                String num = zhuang_info.getNum() + "";
                String peifu = "0";
                if (!StringUtils.isEmpty(zhuang_info.getShuying()))
                    peifu = Float.parseFloat(zhuang_info.getShuying()) / 100 + "";
                zTextView.setText(SpannableStringUtils.getBuilder("恭喜")
                        .setForegroundColor(act.getResources().getColor(R.color.c_5c5c5e))
                        .append("[" + zhuang_info.getNickname() + "]")
                        .setForegroundColor(act.getResources().getColor(R.color.c_fd453b))
                        .append(" ").append(hb_money)
                        .setForegroundColor(act.getResources().getColor(R.color.c_fd453b))
                        .append(" /").append(num + "点")
                        .setForegroundColor(act.getResources().getColor(R.color.c_5c5c5e))
                        .append(" ").append(peifu)
                        .setForegroundColor(act.getResources().getColor(R.color.c_5c5c5e))
                        .create());
                holder.llZJList.addView(zTextView);
                for (int i = 0; i < xiazhu_info.size(); i++) {
                    ErBaQzM2Bean.DataBean.XiazhuInfoBean dataBean = xiazhu_info.get(i);
                    TextView textView = new TextView(act);
                    String Hb_money = "";
                    if (!StringUtils.isEmpty(dataBean.getHb_money()))
                        Hb_money = Float.parseFloat(dataBean.getHb_money()) / 100 + "";
                    int Num = dataBean.getNum();
                    String Peifu = "0";
                    if (!StringUtils.isEmpty(dataBean.getPeifu()))
                        Peifu = Float.parseFloat(dataBean.getPeifu()) / 100 + "";
                    textView.setText(SpannableStringUtils.getBuilder("[" + dataBean.getNickname() + "]")
                            .setForegroundColor(act.getResources().getColor(R.color.c_fd453b))
                            .append(" ").append(Hb_money)
                            .setForegroundColor(act.getResources().getColor(R.color.c_fd453b))
                            .append(" ").append(Num + "点")
                            .setForegroundColor(act.getResources().getColor(R.color.c_5c5c5e))
                            .append(" ").append(dataBean.getRemark())
                            .setForegroundColor(act.getResources().getColor(R.color.c_5c5c5e))
                            .append("(" + Peifu + ")")
                            .setForegroundColor(act.getResources().getColor(R.color.c_5c5c5e))
                            .create());
                    holder.llZJList.addView(textView);
                }
            }
        }
        return convertView;
    }

    public class ViewHolder {

        private ImageView ivRedRight;
        private ImageView ivRedLeft;
        private LinearLayout llRedBoxRight;
        private TextView tvRedMoneyRight;
        private TextView tvRedNameRight;
        private ImageView ivRedHeadRight;
        private RelativeLayout rlRedRight;
        private LinearLayout llZJList;
        private TextView tvRedMoneyLeft;
        private LinearLayout llRedBoxLeft;
        private TextView tvRedNameLeft;
        private ImageView ivRedHeadLeft;
        private RelativeLayout rlRedLeft;

        public ViewHolder(View view) {
            rlRedLeft = (RelativeLayout) view.findViewById(R.id.rl_red_left);
            rlRedRight = (RelativeLayout) view.findViewById(R.id.rl_red_right);
            ivRedHeadLeft = (ImageView) view.findViewById(R.id.iv_red_head_left);
            ivRedHeadRight = (ImageView) view.findViewById(R.id.iv_red_head_right);
            tvRedNameLeft = (TextView) view.findViewById(R.id.tv_red_name_left);
            tvRedNameRight = (TextView) view.findViewById(R.id.tv_red_name_right);
            llRedBoxLeft = (LinearLayout) view.findViewById(R.id.ll_red_box_left);
            llRedBoxRight = (LinearLayout) view.findViewById(R.id.ll_red_box_right);
            tvRedMoneyLeft = (TextView) view.findViewById(R.id.tv_red_money_left);
            tvRedMoneyRight = (TextView) view.findViewById(R.id.tv_money_right);
            ivRedLeft = (ImageView) view.findViewById(R.id.iv_red_left);
            ivRedRight = (ImageView) view.findViewById(R.id.iv_red_right_erba);
            llZJList = (LinearLayout) view.findViewById(R.id.ll_zj_list);
        }
    }
}
